package fr.umlv.android.caveAVin.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import fr.umlv.android.caveAVin.activities.R;
import fr.umlv.android.caveAVin.activities.R.id;
import fr.umlv.android.caveAVin.activities.R.layout;
import fr.umlv.android.caveAVin.beans.User;
import fr.umlv.android.caveAvin.sqlite.UserBDD;

public class CreateUser extends Activity {
	
	EditText loginField;
	EditText passWordField;
	EditText confirmPassWordField;
	EditText mailField;
	Button createOK;
	Button retour;
	TextView errorCreate;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_user);
        loginField = (EditText) findViewById(R.id.Login);
        passWordField = (EditText) findViewById(R.id.PassWord);
        confirmPassWordField = (EditText) findViewById(R.id.ConfirmPassWord);
        mailField = (EditText) findViewById(R.id.Mail);
        createOK = (Button) findViewById(R.id.CreateOK);
        retour = (Button) findViewById(R.id.Retour);
        errorCreate = (TextView) findViewById(R.id.ErrorCreate);
        
//        final UserBDD userBDD = new UserBDD(this);
//        userBDD.open();
        
        createOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				String login = loginField.getText().toString();
				String pass  = passWordField.getText().toString();
				String confirmPass = confirmPassWordField.getText().toString();
				String mail  = mailField.getText().toString();
				
				// verifie s'il ya des champs vide
				if (login.equals("") && pass.equals("") && mail.equals("")) {
					errorCreate.setText("Creation Error, All fields must be completed !!!");
				}
				else {
					// si le pass et sa confirmation sont pas les memes
					if (! pass.equals(confirmPass)) {
						errorCreate.setText("Password Error,  !!!");
					} else { // le cas ou tout est OK
						User user = new User(login, pass, mail);
						UserBDD.insertUser(user);
						Intent myIntentCreateUsetToResume = new Intent(CreateUser.this,ResumeCreateUser.class);
						Bundle myData = new Bundle();
						myData.putString("login", login);
						myData.putString("pass", pass);
						myIntentCreateUsetToResume.putExtras(myData);
						startActivity(myIntentCreateUsetToResume);
					}
					
				}
				
			}
		});
        
        
        // le bouton retour. 
        retour.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent myIntentAuthentificationToCreateUser = new Intent(CreateUser.this,AuthentificationMenu.class);
				startActivity(myIntentAuthentificationToCreateUser);
			}
		});
        
    }

}
